/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.process;


import me.panpf.sketch.Sketch;
import me.panpf.sketch.cache.BitmapPool;
import me.panpf.sketch.decode.ResizeCalculator;
import me.panpf.sketch.request.Resize;
import me.panpf.sketch.util.Log;
import me.panpf.sketch.util.Utils;
import me.panpf.sketch.zoom.PixmapBuild;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.Texture;
import ohos.agp.utils.RectFloat;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.util.logging.Level;
import java.util.logging.Logger;

public class ResizeImageProcessor implements ImageProcessor {

    @Override
    public PixmapBuild process(Sketch sketch, PixmapBuild bitmap, Resize resize, boolean lowQualityImage) {
        if (bitmap.getPixelMap().isReleased()) {
            return bitmap;
        }

        if (resize == null || resize.getWidth() == 0 || resize.getHeight() == 0 ||
                (bitmap.getPixelMap().getImageInfo().size.width == resize.getWidth() && bitmap.getPixelMap().getImageInfo().size.height == resize.getHeight())) {
            return bitmap;
        }
        ResizeCalculator resizeCalculator = sketch.getConfiguration().getResizeCalculator();
        ResizeCalculator.Mapping mapping = resizeCalculator.calculator(bitmap.getPixelMap().getImageInfo().size.width, bitmap.getPixelMap().getImageInfo().size.height,
                resize.getWidth(), resize.getHeight(), Utils.coverScaleMode(resize.getScaleType()), true);


        PixelFormat config = bitmap.getPixelMap().getImageInfo().pixelFormat;
        if (config == null) {
//            config = lowQualityImage ? Bitmap.Config.ARGB_4444 : Bitmap.Config.ARGB_8888;
            config = lowQualityImage ? PixelFormat.UNKNOWN : PixelFormat.ARGB_8888;
        }
        BitmapPool bitmapPool = sketch.getConfiguration().getBitmapPool();
        PixmapBuild resizeBitmap = bitmapPool.getOrMake(mapping.imageWidth, mapping.imageHeight, config);

        Canvas canvas = new Canvas(new Texture(resizeBitmap.getPixelMap()));
        canvas.drawPixelMapHolderRect(new PixelMapHolder(bitmap.getPixelMap()), new RectFloat(mapping.srcRect), new RectFloat(mapping.destRect), new Paint());
//        canvas.drawBitmap(bitmap, mapping.srcRect, mapping.destRect, null);
//        canvas.drawPixelMapHolder(new PixelMapHolder(bitmap), mapping.srcRect.left, mapping.srcRect.top, new Paint());
        return resizeBitmap;
    }

    @Override
    public String toString() {
        return "ResizeImageProcessor";
    }

    @Override
    public String getKey() {
        return "Resize";
    }
}
